#include<iostream>
#include<unistd.h>
#include<signal.h>
#include<sys/wait.h>
#include<sys/types.h>

using namespace std;

void work()
{
    cout<<"print log"<<endl;
}

void handler(int signo)
{
    work();
    cout<<"get errno"<<signo<<endl;
    alarm(5);
}

int main()
{
    signal(SIGALRM,handler);
    int n = alarm(5);

    while(1)
    {
        cout<<"proc is running"<<endl;
        sleep(1);
    }
    

    //终止信号
    pid_t id = fork();
    if(id == 0)
    {
        //child
        int cnt  =5;
        while(cnt)
        {
            cout<<"i am achild proc,pid:"<<getpid()<<endl;
            sleep(1);
            cnt--;
        }
        exit(0);
    }

    //father
    int status = 0;
    pid_t rid = waitpid(id,&status,0);
    if(rid == id)
    {
        cout<<"child quit info,rid "<<rid<<"exit code "<<((status>>8)&0xFF)<<"exit signal "<<(status&0x7F)<<"core dump "<<((status>>7)&1)<<endl;
    }

    return 0;
}
